package dbops

import (
	"database/sql"
	"gitee.com/DengAnbang/Computation/src/bean"
	"gitee.com/DengAnbang/goutils/dbutils"
	"gitee.com/DengAnbang/goutils/loge"
	"gitee.com/DengAnbang/goutils/fileUtil"
)

//查询模型树的结构
func SelectModelTree(dbPath string) ([]bean.ModelTree, error) {
	if !fileUtil.PathExists(dbPath) {
		return nil, bean.NewErrorMessage("模型文件不存在").SetDeBugMessage(dbPath)
	}
	var treeList = make([]bean.ModelTree, 0, 8)
	db, err := sql.Open("sqlite3", dbPath)
	if err != nil {
		return treeList, err
	}
	defer db.Close()
	err = db.Ping()
	if err != nil {
		return treeList, err
	}
	stmt, err := db.Prepare("SELECT * FROM model_tree")
	if err != nil {
		return treeList, err
	}
	defer stmt.Close()
	result, err := stmt.Query()
	if err != nil {
		return treeList, err
	}
	defer result.Close()

	for result.Next() {
		var strings map[string]string
		strings, err = dbutils.GetRowsMap(result)
		if err == nil {
			modelTree := bean.ModelTree{
				Id:   strings["id"],
				Pid:  strings["pid"],
				Name: strings["name"],
				Flag: strings["flag"],
				Tid:  strings["tid"],
			}
			treeList = append(treeList, modelTree)
		} else {
			loge.W(err)
		}
	}
	return treeList, err
}

//查询模型树的结构的附加属性
func SelectModelNodeAddition(dbPath, nodeId string) ([]bean.ModelNodeAddition, error) {
	if len(nodeId) <= 0 {
		return nil, bean.NewErrorMessage("模型的节点不能为空").SetDeBugMessage(dbPath)
	}
	additions := make([]bean.ModelNodeAddition, 0)
	if !fileUtil.PathExists(dbPath) {
		return nil, bean.NewErrorMessage("模型文件不存在").SetDeBugMessage(dbPath)
	}
	db, err := sql.Open("sqlite3", dbPath)
	if err != nil {
		return additions, err
	}
	defer db.Close()
	err = db.Ping()
	if err != nil {
		return additions, err
	}
	stmt, err := db.Prepare("SELECT * FROM model_property WHERE id =?")
	if err != nil {
		return additions, err
	}
	defer stmt.Close()
	result, err := stmt.Query(nodeId)
	if err != nil {
		return additions, err
	}
	defer result.Close()

	for result.Next() {
		var strings map[string]string
		strings, err = dbutils.GetRowsMap(result)
		if err == nil {
			modelNodeAddition := bean.ModelNodeAddition{
				Id:            strings["id"],
				Name:          strings["name"],
				Value:         strings["value"],
				PropertyGroup: strings["propertygroup"],
				Description:   strings["description"],
			}
			additions = append(additions, modelNodeAddition)
		} else {
			loge.W(err)
		}
	}
	return additions, err
}
func B3DFileUpload(modelId, nodeId, name, path, createdId string) (error) {
	//SelectModelBean()
	stmtIn, err := dbConn.Prepare("INSERT INTO table_tree_model (model_id,node_id,name,path,created_id,versions)VALUES(?,?,?,?,?,?)")
	if err != nil {
		return err
	}
	var versions = 0
	beans, err := SelectModelBean(nodeId)
	if err != nil {
		return err
	}
	if len(beans) > 0 {
		versions = beans[0].Versions + 1
	}
	_, err = stmtIn.Exec(modelId, nodeId, name, path, createdId, versions)
	if err != nil {
		return err
	}
	stmtIn.Close()
	stmtIn, err = dbConn.Prepare("UPDATE  table_tree SET model_id=? WHERE node_id=?")
	if err != nil {
		return err
	}
	_, err = stmtIn.Exec(modelId, nodeId)
	if err != nil {
		return err
	}
	stmtIn.Close()
	return err
}

//查询模板的历史纪录
func SelectModelBean(nodeId string) ([]bean.ModelBean, error) {
	var modelList = make([]bean.ModelBean, 0, 8)
	if len(nodeId) == 0 {
		return modelList, bean.NewErrorMessage("节点ID不能为空")
	}
	var err error
	var stmtOut *sql.Stmt
	stmtOut, err = dbConn.Prepare("SELECT * FROM table_tree_model WHERE node_id = ? ORDER BY versions DESC;")
	if err != nil {
		return modelList, err
	}
	defer stmtOut.Close()
	rows, err := stmtOut.Query(nodeId)
	if err != nil {
		return modelList, err
	}
	defer rows.Close()
	for rows.Next() {
		var strings map[string]string
		strings, err = dbutils.GetRowsMap(rows)
		if err == nil {
			tree := bean.ModelBean{}.SetData(strings)
			modelList = append(modelList, tree)
		} else {
			return modelList, err
		}

	}
	return modelList, err
}
